import javax.sound.sampled.Line;
import java.awt.*;

public class line {
    int x = 360, y = 170;//起始
    int end_x = 0, end_y = 0;//结束
    int dir = 1;//方向
    double line_len = 100;//线长
    double line_len_min = 100;
    double line_len_max = 670;
    double n = 0.15;
    double angle = 0;//角度
    /*线的状态
     0 摇摆     1 抓取    2 碰撞    3 返回*/
    int state;
    Image hook = Toolkit.getDefaultToolkit().getImage("img/hook.png");
    GameWin frame;
    line(GameWin frame)
    {
        this.frame = frame;
    }
    //碰撞判定
    void judge()
    {
        for (Object obj:this.frame.objectList)
        {
            if(end_x>obj.x&&end_x<obj.x+obj.width
                    &&end_y>obj.y&&end_y<obj.y+obj.height)
            {
                state = 3;
                obj.flag = true;
            }
        }

    }

    void line_move(Graphics g)
    {
        angle = Math.PI*n;
        end_x = (int)(x+line_len*Math.cos(angle));
        end_y = (int)(y+line_len*Math.sin(angle));
        g.setColor(Color.darkGray);
        g.drawLine(x-1,y,end_x-1,end_y);
        g.drawLine(x,y,end_x,end_y);
        g.drawLine(x+1,y,end_x+1,end_y);
        g.drawImage(hook,end_x-36,end_y-2,null);
    }

    void print_line(Graphics g)
    {
        judge();
        switch (state)
        {
            case 0://摇摆
                Sky.yaoState = false;//药水状态恢复为未使用
                if(n<0.15)
                {
                    dir = 1;
                }
                else if(n>0.85)
                {
                    dir = -1;
                }
                n = n+0.005*dir;
                line_move(g);
                break;
            case 1://抓取
                if(line_len<line_len_max)
                {
                    line_len = line_len+5;
                    line_move(g);
                }
                else
                {
                    state = 2;
                }
                break;
            case 2://碰撞
                if(line_len>line_len_min)
                {
                    line_len = line_len-5;
                    line_move(g);
                }
                else
                {
                    state = 0;
                }
                break;
            case 3://返回
                int m = 1;
                if(line_len>line_len_min)
                {
                    line_len = line_len-5;
                    line_move(g);
                    for (Object obj:this.frame.objectList)
                    {
                        if(obj.flag)
                        {
                            m = obj.m;
                            obj.x = end_x - obj.getWidth()/2;
                            obj.y = end_y;
                            if(line_len<=line_len_min)
                            {
                                obj.x = -150;
                                obj.y = -150;
                                Sky.total = Sky.total+ obj.money;
                                obj.flag = false;
                                state = 0;
                            }
                        }
                    }
                   if(Sky.yaoState == false)//判断药水状态，若未使用则抓取物体时钩子回收变慢
                   {
                       try {
                           Thread.sleep(m);
                       } catch (InterruptedException e) {
                           throw new RuntimeException(e);
                       }
                   }
                }
                break;
        }
    }
}
